package com.css.fxfzysx.modules.vaolEngineering.repository.impl;

import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxTransformerSubstationEntity;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.modules.vaolEngineering.entity.newEntity.SmxTransformerSubstationValueEntity;
import com.css.fxfzysx.modules.vaolEngineering.param.update.TsParam;
import com.css.fxfzysx.modules.vaolEngineering.param.update.TsValueParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.TsVulnerabilityRepository;
import com.css.fxfzysx.modules.vaolEngineering.vo.lifelineCalculation.SmxTransformerSubstationValueVo;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class TsVulnerabilityRepositoryImpl implements TsVulnerabilityRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public boolean save(SmxTransformerSubstationEntity entity) {
        String sql = "insert into czt_smx_transformer_substation values (:id,:province,:city,:county,:tsId,:name,:position,:longitude,:latitude,:typeOfUbstation,:voltageGrade,:structureType,:fortificationIntensity,:year,:samplesNumber,:pga,:damagesNumber,:substationTotalCost,:equipmentOutdoors,:costOfIndoorEquipment,:costOfBuilding,:site,ST_GeometryFromText(:geom,4490),:versionId,:tableId,:serialNumber,:delFlag,:createUser,:createTime,:updateUser,:updateTime,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10,:type)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public List<SmxTransformerSubstationEntity> getByVersionId(String id) {
        try {
            String sql = "select * from czt_smx_transformer_substation where version_id = '" + id + "'";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SmxTransformerSubstationEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveTsResult(SmxTransformerSubstationValueEntity entity) {
        jdbcTemplate.update("insert into czt_smx_transformer_substation_value  (id,range,pgai,samples_acc,range_count,range_break_count,ril,rir,fi,site_num,fi_correct,ts_id,del_flag) values (?,?,?,?,?,?,?,?,?,?,?,?,? )",
                entity.getId(),entity.getRange(),entity.getPgai(),entity.getSamplesAcc(),entity.getRangeCount(),entity.getRangeBreakCount(),entity.getRil(),entity.getRir(),entity.getFi(),entity.getSiteNum(),entity.getFiCorrect(),entity.getTsId(),entity.getDelFlag());
    }

    @Override
    public List<SmxTransformerSubstationEntity> getByTableId(String id) {
        try {
            String sql = "select * from czt_smx_transformer_substation where table_id = '" + id + "'";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SmxTransformerSubstationEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<SmxTransformerSubstationValueVo> getValueByTsId(String id) {
        try {
            String sql = "select * from czt_smx_transformer_substation_value where ts_id = '" + id + "' and del_flag = '0'";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SmxTransformerSubstationValueVo.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void updateById(TsParam param) {
        jdbcTemplate.update("update czt_smx_transformer_substation set samples_number=?,pga=?,damages_number=? where id=?",param.getSamplesNumber(),param.getPga(),param.getDamagesNumber(),param.getId());
    }

    @Override
    public void delValueByTsId(String tsId) {
        jdbcTemplate.update("delete from czt_smx_transformer_substation_value  where ts_id = '"+tsId+"'");
    }

    @Override
    public void saveResult(TsValueParam entity) {
        jdbcTemplate.update("insert into czt_smx_transformer_substation_value  (id,range,pgai,samples_acc,range_count,range_break_count,ril,rir,fi,site_num,fi_correct,ts_id,del_flag) values (?,?,?,?,?,?,?,?,?,?,?,?,? )",
                entity.getId(),entity.getRange(),entity.getPgai(),entity.getSamplesAcc(),entity.getRangeCount(),entity.getRangeBreakCount(),entity.getRil(),entity.getRir(),entity.getFi(),entity.getSiteNum(),entity.getFiCorrect(),entity.getTsId(),entity.getDelFlag());
    }

    @Override
    public DestructiveStateVo getByBasicInfo(SmxTransformerSubstationEntity entity,String year) {
        try {
            StringBuilder sql = new StringBuilder("select * from czt_smx_basic_library_substation where del_flag = '0' and year = '" + year + "' and type = '"+entity.getType()+"' and range = " + 0);
            return jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DestructiveStateVo.class));
        } catch (DataAccessException e) {
            return new DestructiveStateVo();
        }
    }
}
